#define BLINKER_WIFI
#define BLINKER_MIOT_LIGHT

#define BLINKER_DUEROS_LIGHT

#define BLINKER_ALIGENIE_LIGHT

#define BLINKER_WITHOUT_SSL

#include <Blinker.h>
#include <FastLED.h>

char auth[] = "4ad999b0ace1";
char ssid[] = "Sweet_meng";
char pswd[] = "aita0116.";

BlinkerButton Button1("k1");
BlinkerButton Button2("k2");
BlinkerButton Button3("k3");
BlinkerButton Button4("k4");
BlinkerButton Button5("k5");
BlinkerSlider Slider1("hk1"); //亮度
BlinkerSlider Slider2("hk2"); //频率
BlinkerRGB RGBWS2812("RGB1");

#define USB_F2 2
#define PIN 0                //ws2812 DAT 接的引脚编号
#define NUM_LEDS 8          //ws2812 灯数
#define LED_TYPE WS2812B     //芯片型号FastLED
#define COLOR_ORDER GRB      //红绿蓝顺序FastLED
int UPDATES_PER_SECOND = 50; //每秒刷新频率FastLED
int BRIGHTNESS = 255;        //亮度值FastLED
int openState = 0;
int32_t colorT;
bool wsState;
uint8_t XMwsMode = BLINKER_CMD_MIOT_DAY;
String XDwsMode = BLINKER_CMD_DUEROS_READING;
String TMwsMode = BLINKER_CMD_ALIGENIE_READING;
uint8_t colorR, colorG, colorB, colorW; //rgb以及亮度
CRGB leds[NUM_LEDS];                    //引入灯数FastLED
CRGBPalette16 currentPalette;
TBlendType currentBlending;
extern CRGBPalette16 myRedWhiteBluePalette;
extern const TProgmemPalette16 myRedWhiteBluePalette_p PROGMEM;

void heartbeat()
{
    BLINKER_LOG("heartbeat,state: ", openState);
    if (openState == 0)
    {
        Button1.print("off");
        Button2.print("off");
        Button3.print("off");
        Button4.print("off");
        Button5.print("off");
    }
    else if (openState == 1)
    {
        Button1.print("on");
        Button2.print("off");
        Button3.print("off");
        Button4.print("off");
        Button5.print("off");
    }
    else if (openState == 2)
    {
        Button1.print("off");
        Button2.print("on");
        Button3.print("off");
        Button4.print("off");
        Button5.print("off");
    }
    else if (openState == 3)
    {
        Button1.print("off");
        Button2.print("off");
        Button3.print("on");
        Button4.print("off");
        Button5.print("off");
    }
    else if (openState == 4)
    {
        Button1.print("off");
        Button2.print("off");
        Button3.print("off");
        Button4.print("on");
        Button5.print("off");
    }
    else if (openState == 5)
    {
        Button1.print("off");
        Button2.print("off");
        Button3.print("off");
        Button4.print("off");
        Button5.print("on");
    }
    Slider1.print(BRIGHTNESS);
    Slider2.print(UPDATES_PER_SECOND);
    RGBWS2812.print(colorR, colorG, colorB, BRIGHTNESS);
}

void button1_callback(const String &state)
{
    BLINKER_LOG("get button state: ", state);
    if (state == BLINKER_CMD_ON)
    {
        BLINKER_LOG("纯色调光模式开启!");
        wsState = true;
        colorR = 255;
        colorG = 255;
        colorB = 255;
        BRIGHTNESS = 255;
        openState = 1;
        Button1.print("on");
        Button2.print("off");
        Button3.print("off");
        Button4.print("off");
        Button5.print("off");
    }
    else if (state == BLINKER_CMD_OFF)
    {
        BLINKER_LOG("纯色调光模式关闭!");
        wsState = false;
        openState = 0;
        Button1.print("off");
        Button2.print("off");
        Button3.print("off");
        Button4.print("off");
        Button5.print("off");
    }
    BlinkerMIOT.powerState(wsState ? "on" : "off");
    BlinkerMIOT.print();

    BlinkerDuerOS.powerState(wsState ? "on" : "off");
    BlinkerDuerOS.print();

    BlinkerAliGenie.powerState(wsState ? "on" : "off");
    BlinkerAliGenie.print();
}

void button2_callback(const String &state)
{
    BLINKER_LOG("get button state: ", state);
    if (state == BLINKER_CMD_ON)
    {
        BLINKER_LOG("日光/阅读模式开启!");
        wsState = true;
        openState = 2;
        Button1.print("off");
        Button2.print("on");
        Button3.print("off");
        Button4.print("off");
        Button5.print("off");
    }
    else if (state == BLINKER_CMD_OFF)
    {
        BLINKER_LOG("日光/阅读模式关闭!");
        wsState = false;
        openState = 0;
        Button1.print("off");
        Button2.print("off");
        Button3.print("off");
        Button4.print("off");
        Button5.print("off");
    }
    BlinkerMIOT.powerState(wsState ? "on" : "off");
    BlinkerMIOT.print();

    BlinkerDuerOS.powerState(wsState ? "on" : "off");
    BlinkerDuerOS.print();

    BlinkerAliGenie.powerState(wsState ? "on" : "off");
    BlinkerAliGenie.print();
}

void button3_callback(const String &state)
{
    BLINKER_LOG("get button state: ", state);
    if (state == BLINKER_CMD_ON)
    {
        BLINKER_LOG("月光/影院模式开启!");
        wsState = true;
        openState = 3;
        Button1.print("off");
        Button2.print("off");
        Button3.print("on");
        Button4.print("off");
        Button5.print("off");
    }
    else if (state == BLINKER_CMD_OFF)
    {
        BLINKER_LOG("月光/影院模式关闭!");
        wsState = false;
        openState = 0;
        Button1.print("off");
        Button2.print("off");
        Button3.print("off");
        Button4.print("off");
        Button5.print("off");
    }
    BlinkerMIOT.powerState(wsState ? "on" : "off");
    BlinkerMIOT.print();

    BlinkerDuerOS.powerState(wsState ? "on" : "off");
    BlinkerDuerOS.print();

    BlinkerAliGenie.powerState(wsState ? "on" : "off");
    BlinkerAliGenie.print();
}

void button4_callback(const String &state)
{
    BLINKER_LOG("get button state: ", state);
    if (state == BLINKER_CMD_ON)
    {
        BLINKER_LOG("彩光/音乐模式开启!");
        wsState = true;
        openState = 4;
        Button1.print("off");
        Button2.print("off");
        Button3.print("off");
        Button4.print("on");
        Button5.print("off");
    }
    else if (state == BLINKER_CMD_OFF)
    {
        BLINKER_LOG("彩光/音乐模式关闭!");
        wsState = false;
        openState = 0;
        Button1.print("off");
        Button2.print("off");
        Button3.print("off");
        Button4.print("off");
        Button5.print("off");
    }
    BlinkerMIOT.powerState(wsState ? "on" : "off");
    BlinkerMIOT.print();

    BlinkerDuerOS.powerState(wsState ? "on" : "off");
    BlinkerDuerOS.print();

    BlinkerAliGenie.powerState(wsState ? "on" : "off");
    BlinkerAliGenie.print();
}

void button5_callback(const String &state)
{
    BLINKER_LOG("get button state: ", state);
    if (state == BLINKER_CMD_ON)
    {
        BLINKER_LOG("温馨/假日模式开启!");
        wsState = true;
        openState = 5;
        Button1.print("off");
        Button2.print("off");
        Button3.print("off");
        Button4.print("off");
        Button5.print("on");
    }
    else if (state == BLINKER_CMD_OFF)
    {
        BLINKER_LOG("温馨/假日模式关闭!");
        wsState = false;
        openState = 0;
        Button1.print("off");
        Button2.print("off");
        Button3.print("off");
        Button4.print("off");
        Button5.print("off");
    }
    BlinkerMIOT.powerState(wsState ? "on" : "off");
    BlinkerMIOT.print();

    BlinkerDuerOS.powerState(wsState ? "on" : "off");
    BlinkerDuerOS.print();

    BlinkerAliGenie.powerState(wsState ? "on" : "off");
    BlinkerAliGenie.print();
}
void slider1_callback(int32_t value)
{
    BLINKER_LOG("get slider value: ", value);
    BRIGHTNESS = value;
    RGBWS2812.print(colorR, colorG, colorB, BRIGHTNESS);
}

void slider2_callback(int32_t value)
{
    BLINKER_LOG("get slider value: ", value);
    UPDATES_PER_SECOND = value;
}

void ws2812_callback(uint8_t r_value, uint8_t g_value, uint8_t b_value, uint8_t bright_value)
{
    BLINKER_LOG("R value: ", r_value);
    BLINKER_LOG("G value: ", g_value);
    BLINKER_LOG("B value: ", b_value);
    BLINKER_LOG("Brightness value: ", bright_value); //颜色和亮度赋值
    colorR = r_value;
    colorG = g_value;
    colorB = b_value;
    BRIGHTNESS = bright_value;
    Slider1.print(BRIGHTNESS);
    Button1.print("on");
    Button2.print("off");
    Button3.print("off");
    Button4.print("off");
    Button5.print("off");
    openState = 1;
}

void RGBWShow()
{
    FastLED.setBrightness(BRIGHTNESS);
    for (int i = 0; i < NUM_LEDS; i++)
    {
        leds[i] = CRGB(colorR, colorG, colorB);
    }
    FastLED.show();
}

uint32_t XMgetColor()
{
    uint32_t color = colorR << 16 | colorG << 8 | colorB;
    return color;
}

void miotPowerState(const String &state)
{
    BLINKER_LOG("need set power state: ", state);
    if (state == BLINKER_CMD_ON)
    {
        wsState = true;
        colorR = 255;
        colorG = 255;
        colorB = 255;
        BRIGHTNESS = 255;
        openState = 1;
        Button1.print("on");
        Button2.print("off");
        Button3.print("off");
        Button4.print("off");
        Button5.print("off");
    }
    else if (state == BLINKER_CMD_OFF)
    {
        wsState = false;
        openState = 0;
        Button1.print("off");
        Button2.print("off");
        Button3.print("off");
        Button4.print("off");
        Button5.print("off");
    }
    BlinkerMIOT.powerState(wsState ? "on" : "off");
    BlinkerMIOT.brightness(BRIGHTNESS * 100 / 255);
    BlinkerMIOT.print();
}

void miotColor(int32_t color)
{
    BLINKER_LOG("need set color: ", color);
    colorR = color >> 16 & 0xFF;
    colorG = color >> 8 & 0xFF;
    colorB = color & 0xFF;
    BLINKER_LOG("colorR: ", colorR, ", colorG: ", colorG, ", colorB: ", colorB);
    openState = 1;
    BlinkerMIOT.color(color);
    BlinkerMIOT.print();
}

void miotMode(uint8_t mode)
{
    BLINKER_LOG("need set mode: ", mode);
    if (mode == BLINKER_CMD_MIOT_DAY)
    {
        BlinkerMIOT.powerState("on");
        openState = 2;
        Button1.print("off");
        Button2.print("on");
        Button3.print("off");
        Button4.print("off");
        Button5.print("off");
    }
    else if (mode == BLINKER_CMD_MIOT_NIGHT)
    {
        BlinkerMIOT.powerState("on");
        openState = 3;
        Button1.print("off");
        Button2.print("off");
        Button3.print("on");
        Button4.print("off");
        Button5.print("off");
    }
    else if (mode == BLINKER_CMD_MIOT_COLOR)
    {
        BlinkerMIOT.powerState("on");
        openState = 4;
        Button1.print("off");
        Button2.print("off");
        Button3.print("off");
        Button4.print("on");
        Button5.print("off");
    }
    else if (mode == BLINKER_CMD_MIOT_WARMTH)
    {
        BlinkerMIOT.powerState("on");
        openState = 5;
        Button1.print("off");
        Button2.print("off");
        Button3.print("off");
        Button4.print("off");
        Button5.print("on");
    }
    else if (mode == BLINKER_CMD_MIOT_TV)
    {
        // Your mode function
    }
    else if (mode == BLINKER_CMD_MIOT_READING)
    {
        // Your mode function
    }
    else if (mode == BLINKER_CMD_MIOT_COMPUTER)
    {
        // Your mode function
    }
    XMwsMode = mode;
    BlinkerMIOT.mode(XMwsMode);
    BlinkerMIOT.print();
}

void miotColoTemp(int32_t colorTemp)
{
    BLINKER_LOG("need set colorTemperature: ", colorTemp);
    colorT = colorTemp;
    BlinkerMIOT.colorTemp(colorT);
    BlinkerMIOT.print();
}
void miotBright(const String &bright)
{
    BLINKER_LOG("need set brightness: ", bright);
    BRIGHTNESS = bright.toInt() * 255 / 100;
    BLINKER_LOG("now set brightness: ", BRIGHTNESS);
    BlinkerMIOT.brightness(BRIGHTNESS * 100 / 255);
    Slider1.print(BRIGHTNESS);
    RGBWS2812.print(colorR, colorG, colorB, BRIGHTNESS);
    BlinkerMIOT.print();
}

void miotQuery(int32_t queryCode)
{
    BLINKER_LOG("MIOT Query codes: ", queryCode);
    switch (queryCode)
    {
    case BLINKER_CMD_QUERY_ALL_NUMBER:
        BLINKER_LOG("MIOT Query All");
        BlinkerMIOT.powerState(wsState ? "on" : "off");
        BlinkerMIOT.color(XMgetColor());
        BlinkerMIOT.mode(XMwsMode);
        BlinkerMIOT.colorTemp(colorT);
        BlinkerMIOT.brightness(BRIGHTNESS * 100 / 255);
        BlinkerMIOT.print();
        break;
    case BLINKER_CMD_QUERY_POWERSTATE_NUMBER:
        BLINKER_LOG("MIOT Query Power State");
        BlinkerMIOT.powerState(wsState ? "on" : "off");
        BlinkerMIOT.print();
        break;
    case BLINKER_CMD_QUERY_COLOR_NUMBER:
        BLINKER_LOG("MIOT Query Color");
        BlinkerMIOT.color(XMgetColor());
        BlinkerMIOT.print();
        break;
    case BLINKER_CMD_QUERY_MODE_NUMBER:
        BLINKER_LOG("MIOT Query Mode");
        BlinkerMIOT.mode(XMwsMode);
        BlinkerMIOT.print();
        break;
    case BLINKER_CMD_QUERY_COLORTEMP_NUMBER:
        BLINKER_LOG("MIOT Query ColorTemperature");
        BlinkerMIOT.colorTemp(colorT);
        BlinkerMIOT.print();
        break;
    case BLINKER_CMD_QUERY_BRIGHTNESS_NUMBER:
        BLINKER_LOG("MIOT Query Brightness");
        BlinkerMIOT.brightness(BRIGHTNESS * 100 / 255);
        BlinkerMIOT.print();
        break;
    default:
        BlinkerMIOT.powerState(wsState ? "on" : "off");
        BlinkerMIOT.color(XMgetColor());
        BlinkerMIOT.mode(XMwsMode);
        BlinkerMIOT.colorTemp(colorT);
        BlinkerMIOT.brightness(BRIGHTNESS * 100 / 255);
        BlinkerMIOT.print();
        break;
    }
}
uint32_t XDgetColor()
{
    uint32_t color = colorR << 16 | colorG << 8 | colorB;
    //    switch (color)
    //    {
    //    case 0xFF0000:
    //        return "Red";
    //    case 0xFFFF00:
    //        return "Yellow";
    //    case 0x0000FF:
    //        return "Blue";
    //    case 0x00FF00:
    //        return "Green";
    //    case 0xFFFFFF:
    //        return "White";
    //    case 0x000000:
    //        return "Black";
    //    case 0x00FFFF:
    //        return "Cyan";
    //    case 0x800080:
    //        return "Purple";
    //    case 0xFFA500:
    //        return "Orange";
    //    default:
    //        return "White";
    //    }
    return color;
}
void duerPowerState(const String &state)
{
    BLINKER_LOG("need set power state: ", state);
    if (state == BLINKER_CMD_ON)
    {
        wsState = true;
        colorR = 255;
        colorG = 255;
        colorB = 255;
        BRIGHTNESS = 255;
        openState = 1;
        Button1.print("on");
        Button2.print("off");
        Button3.print("off");
        Button4.print("off");
        Button5.print("off");
    }
    else if (state == BLINKER_CMD_OFF)
    {
        wsState = false;
        openState = 0;
        Button1.print("off");
        Button2.print("off");
        Button3.print("off");
        Button4.print("off");
        Button5.print("off");
    }
    BlinkerDuerOS.powerState(wsState ? "on" : "off");
    BlinkerDuerOS.brightness(BRIGHTNESS * 100 / 255);
    BlinkerDuerOS.print();
}

void duerColor(int32_t color)
{
    BLINKER_LOG("need set color: ", color);
    colorR = color >> 16 & 0xFF;
    colorG = color >> 8 & 0xFF;
    colorB = color & 0xFF;
    BLINKER_LOG("colorR: ", colorR, ", colorG: ", colorG, ", colorB: ", colorB);
    openState = 1;
    BlinkerDuerOS.color(color);
    BlinkerDuerOS.print();
}

void duerMode(const String &mode)
{
    // READING：阅读
    // SLEEP：睡眠
    // ALARM：报警
    // NIGHT_LIGHT：夜灯
    // ROMANTIC：浪漫
    // SUNDOWN：日落
    // SUNRISE：日出
    // RELAX ：休闲/放松
    // LIGHTING ：照明
    // SUN ：太阳
    // STAR ：星星
    // ENERGY_SAVING：节能
    // MOON：月亮
    // JUDI：蹦迪
    BLINKER_LOG("need set mode: ", mode);

    if (mode == BLINKER_CMD_DUEROS_READING)
    {
        // Your cmode function
    }
    else if (mode == BLINKER_CMD_DUEROS_SLEEP)
    {
        // Your cmode function
    }
    else if (mode == BLINKER_CMD_DUEROS_ALARM)
    {
        // Your cmode function
    }
    else if (mode == BLINKER_CMD_DUEROS_NIGHT_LIGHT)
    {
        // Your cmode function
    }
    else if (mode == BLINKER_CMD_DUEROS_ROMANTIC)
    {
        // Your cmode function
    }
    else if (mode == BLINKER_CMD_DUEROS_SUNDOWN)
    {
        // Your cmode function
    }
    else if (mode == BLINKER_CMD_DUEROS_SUNRISE)
    {
        // Your cmode function
    }
    else if (mode == BLINKER_CMD_DUEROS_RELAX)
    {
        // Your mode function
    }
    else if (mode == BLINKER_CMD_DUEROS_LIGHTING)
    {
        // Your mode function
    }

    else if (mode == BLINKER_CMD_DUEROS_SUN)
    {
        // Your mode function
        BlinkerDuerOS.powerState("on");
        openState = 2;
        Button1.print("off");
        Button2.print("on");
        Button3.print("off");
        Button4.print("off");
        Button5.print("off");
    }
    else if (mode == BLINKER_CMD_DUEROS_STAR)
    {
        // Your mode function
        BlinkerDuerOS.powerState("on");
        openState = 5;
        Button1.print("off");
        Button2.print("off");
        Button3.print("off");
        Button4.print("on");
        Button5.print("off");
    }
    else if (mode == BLINKER_CMD_DUEROS_ENERGY_SAVING)
    {
        // Your mode function
    }
    else if (mode == BLINKER_CMD_DUEROS_MOON)
    {
        // Your mode function
        BlinkerDuerOS.powerState("on");
        openState = 3;
        Button1.print("off");
        Button2.print("off");
        Button3.print("on");
        Button4.print("off");
        Button5.print("off");
    }
    else if (mode == BLINKER_CMD_DUEROS_JUDI)
    {
        // Your mode function
        BlinkerDuerOS.powerState("on");
        openState = 4;
        Button1.print("off");
        Button2.print("off");
        Button3.print("off");
        Button4.print("off");
        Button5.print("on");
    }

    XDwsMode = mode;
    BlinkerDuerOS.mode(XDwsMode);
    BlinkerDuerOS.print();
}

void duercMode(const String &cmode)
{
    BLINKER_LOG("need cancel mode: ", cmode);

    if (cmode == BLINKER_CMD_DUEROS_READING)
    {
        // Your cmode function
    }
    else if (cmode == BLINKER_CMD_DUEROS_SLEEP)
    {
        // Your cmode function
    }
    else if (cmode == BLINKER_CMD_DUEROS_ALARM)
    {
        // Your cmode function
    }
    else if (cmode == BLINKER_CMD_DUEROS_NIGHT_LIGHT)
    {
        // Your cmode function
    }
    else if (cmode == BLINKER_CMD_DUEROS_ROMANTIC)
    {
        // Your cmode function
    }
    else if (cmode == BLINKER_CMD_DUEROS_SUNDOWN)
    {
        // Your cmode function
    }
    else if (cmode == BLINKER_CMD_DUEROS_SUNRISE)
    {
        // Your cmode function
    }
    else if (cmode == BLINKER_CMD_DUEROS_RELAX)
    {
        // Your cmode function
    }
    else if (cmode == BLINKER_CMD_DUEROS_LIGHTING)
    {
        // Your cmode function
    }
    else if (cmode == BLINKER_CMD_DUEROS_SUN)
    {
        // Your cmode function
        BlinkerDuerOS.powerState("on");
        wsState = true;
        colorR = 255;
        colorG = 255;
        colorB = 255;
        BRIGHTNESS = 255;
        openState = 1;
        Button1.print("on");
        Button2.print("off");
        Button3.print("off");
        Button4.print("off");
        Button5.print("off");
    }
    else if (cmode == BLINKER_CMD_DUEROS_STAR)
    {
        // Your cmode function
        BlinkerDuerOS.powerState("on");
        wsState = true;
        colorR = 255;
        colorG = 255;
        colorB = 255;
        BRIGHTNESS = 255;
        openState = 1;
        Button1.print("on");
        Button2.print("off");
        Button3.print("off");
        Button4.print("off");
        Button5.print("off");
    }
    else if (cmode == BLINKER_CMD_DUEROS_ENERGY_SAVING)
    {
        // Your cmode function
    }
    else if (cmode == BLINKER_CMD_DUEROS_MOON)
    {
        // Your cmode function
        BlinkerDuerOS.powerState("on");
        wsState = true;
        colorR = 255;
        colorG = 255;
        colorB = 255;
        BRIGHTNESS = 255;
        openState = 1;
        Button1.print("on");
        Button2.print("off");
        Button3.print("off");
        Button4.print("off");
        Button5.print("off");
    }
    else if (cmode == BLINKER_CMD_DUEROS_JUDI)
    {
        // Your cmode function
        BlinkerDuerOS.powerState("on");
        wsState = true;
        colorR = 255;
        colorG = 255;
        colorB = 255;
        BRIGHTNESS = 255;
        openState = 1;
        Button1.print("on");
        Button2.print("off");
        Button3.print("off");
        Button4.print("off");
        Button5.print("off");
    }

    XDwsMode = cmode;
    BlinkerDuerOS.mode(XDwsMode);
    BlinkerDuerOS.brightness(BRIGHTNESS * 100 / 255);
    BlinkerDuerOS.print();
}

void duerBright(const String &bright)
{
    BLINKER_LOG("need set brightness: ", bright);
    if (bright == BLINKER_CMD_MAX)
    {
        BRIGHTNESS = 255;
    }
    else if (bright == BLINKER_CMD_MIN)
    {
        BRIGHTNESS = 0;
    }
    else
    {
        BRIGHTNESS = bright.toInt() * 255 / 100;
    }
    BLINKER_LOG("now set brightness: ", BRIGHTNESS);
    BlinkerDuerOS.brightness(BRIGHTNESS * 100 / 255);
    Slider1.print(BRIGHTNESS);
    RGBWS2812.print(colorR, colorG, colorB, BRIGHTNESS);
    BlinkerDuerOS.print();
}

void duerRelativeBright(int32_t bright)
{
    BLINKER_LOG("need set relative brightness: ", bright);
    if (BRIGHTNESS + bright < 255 && BRIGHTNESS + bright >= 0)
    {
        BRIGHTNESS += bright;
    }
    BLINKER_LOG("now set brightness: ", BRIGHTNESS);
    BlinkerDuerOS.brightness(BRIGHTNESS * 100 / 255);
    Slider1.print(BRIGHTNESS);
    RGBWS2812.print(colorR, colorG, colorB, BRIGHTNESS);
    BlinkerDuerOS.print();
}

void duerColoTemp(int32_t colorTemp)
{
    BLINKER_LOG("need set colorTemperature: ", colorTemp);
    colorT = colorTemp;
    BlinkerDuerOS.colorTemp(colorT);
    BlinkerDuerOS.print();
}

void duerRelativeColoTemp(int32_t colorTemp)
{
    BLINKER_LOG("need set relative colorTemperature: ", colorTemp);
    colorT = colorTemp;
    BlinkerDuerOS.colorTemp(colorT);
    BlinkerDuerOS.print();
}

void duerQuery(int32_t queryCode)
{
    BLINKER_LOG("DuerOS Query codes: ", queryCode);
    switch (queryCode)
    {
    case BLINKER_CMD_QUERY_ALL_NUMBER:
        BLINKER_LOG("DuerOS Query All");
        BlinkerDuerOS.powerState(wsState ? "on" : "off");
        BlinkerDuerOS.color(XDgetColor());
        BlinkerDuerOS.mode(XDwsMode);
        BlinkerDuerOS.brightness(BRIGHTNESS * 100 / 255);
        BlinkerDuerOS.colorTemp(colorT);
        BlinkerDuerOS.time(millis());
        BlinkerDuerOS.print();
        break;
    case BLINKER_CMD_QUERY_POWERSTATE_NUMBER:
        BLINKER_LOG("DuerOS Query Power State");
        BlinkerDuerOS.powerState(wsState ? "on" : "off");
        BlinkerDuerOS.print();
        break;
    case BLINKER_CMD_QUERY_COLOR_NUMBER:
        BLINKER_LOG("DuerOS Query Color");
        BlinkerDuerOS.color(XDgetColor());
        BlinkerDuerOS.print();
        break;
    case BLINKER_CMD_QUERY_MODE_NUMBER:
        BLINKER_LOG("DuerOS Query Mode");
        BlinkerDuerOS.mode(XDwsMode);
        BlinkerDuerOS.print();
        break;

    case BLINKER_CMD_QUERY_BRIGHTNESS_NUMBER:
        BLINKER_LOG("DuerOS Query Brightness");
        BlinkerDuerOS.brightness(BRIGHTNESS * 100 / 255);
        BlinkerDuerOS.print();
        break;
    case BLINKER_CMD_QUERY_COLORTEMP_NUMBER:
        BLINKER_LOG("DuerOS Query ColorTemperature");
        BlinkerDuerOS.colorTemp(colorT);
        BlinkerDuerOS.print();
        break;

    case BLINKER_CMD_QUERY_TIME_NUMBER:
        BLINKER_LOG("DuerOS Query time");
        BlinkerDuerOS.time(millis());
        BlinkerDuerOS.print();
        break;
    default:
        BlinkerDuerOS.powerState(wsState ? "on" : "off");
        BlinkerDuerOS.color(XDgetColor());
        BlinkerDuerOS.mode(XDwsMode);
        BlinkerDuerOS.brightness(BRIGHTNESS * 100 / 255);
        BlinkerDuerOS.colorTemp(colorT);
        BlinkerDuerOS.time(millis());
        BlinkerDuerOS.print();
        break;
    }
}

String TMgetColor()
{
    uint32_t color = colorR << 16 | colorG << 8 | colorB;
    switch (color)
    {
    case 0xFF0000:
        return "Red";
    case 0xFFFF00:
        return "Yellow";
    case 0x0000FF:
        return "Blue";
    case 0x00FF00:
        return "Green";
    case 0xFFFFFF:
        return "White";
    case 0x000000:
        return "Black";
    case 0x00FFFF:
        return "Cyan";
    case 0x800080:
        return "Purple";
    case 0xFFA500:
        return "Orange";
    default:
        return "White";
    }
}
void aligeniePowerState(const String &state)
{
    BLINKER_LOG("need set power state: ", state);
    if (state == BLINKER_CMD_ON)
    {
        wsState = true;
        colorR = 255;
        colorG = 255;
        colorB = 255;
        BRIGHTNESS = 255;
        openState = 1;
        Button1.print("on");
        Button2.print("off");
        Button3.print("off");
        Button4.print("off");
        Button5.print("off");
    }
    else if (state == BLINKER_CMD_OFF)
    {

        wsState = false;
        openState = 0;
        Button1.print("off");
        Button2.print("off");
        Button3.print("off");
        Button4.print("off");
        Button5.print("off");
    }
    BlinkerAliGenie.powerState(wsState ? "on" : "off");
    BlinkerAliGenie.brightness(BRIGHTNESS * 100 / 255);
    BlinkerAliGenie.print();
}

void aligenieColor(const String &color)
{
    BLINKER_LOG("need set color: ", color);
    openState = 1;
    if (color == "Red")
    {
        colorR = 255;
        colorG = 0;
        colorB = 0;
    }
    else if (color == "Yellow")
    {
        colorR = 255;
        colorG = 255;
        colorB = 0;
    }
    else if (color == "Blue")
    {
        colorR = 0;
        colorG = 0;
        colorB = 255;
    }
    else if (color == "Green")
    {
        colorR = 0;
        colorG = 255;
        colorB = 0;
    }
    else if (color == "White")
    {
        colorR = 255;
        colorG = 255;
        colorB = 255;
    }
    else if (color == "Black")
    {
        colorR = 0;
        colorG = 0;
        colorB = 0;
    }
    else if (color == "Cyan")
    {
        colorR = 0;
        colorG = 255;
        colorB = 255;
    }
    else if (color == "Purple")
    {
        colorR = 128;
        colorG = 0;
        colorB = 128;
    }
    else if (color == "Orange")
    {
        colorR = 255;
        colorG = 165;
        colorB = 0;
    }
    BlinkerAliGenie.color(color);
    BlinkerAliGenie.print();
}

void aligenieMode(const String &mode)
{
    BLINKER_LOG("need set mode: ", mode);

    if (mode == BLINKER_CMD_ALIGENIE_READING)
    {
        // Your mode function
        BlinkerAliGenie.powerState("on");
        openState = 2;
        Button1.print("off");
        Button2.print("on");
        Button3.print("off");
        Button4.print("off");
        Button5.print("off");
    }
    else if (mode == BLINKER_CMD_ALIGENIE_MOVIE)
    {
        // Your mode function
        BlinkerAliGenie.powerState("on");
        openState = 3;
        Button1.print("off");
        Button2.print("off");
        Button3.print("on");
        Button4.print("off");
        Button5.print("off");
    }
    else if (mode == BLINKER_CMD_ALIGENIE_SLEEP)
    {
        // Your mode function
    }
    else if (mode == BLINKER_CMD_ALIGENIE_HOLIDAY)
    {
        // Your mode function
        BlinkerAliGenie.powerState("on");
        openState = 5;
        Button1.print("off");
        Button2.print("off");
        Button3.print("off");
        Button4.print("off");
        Button5.print("on");
    }
    else if (mode == BLINKER_CMD_ALIGENIE_MUSIC)
    {
        // Your mode function
        BlinkerAliGenie.powerState("on");
        openState = 4;
        Button1.print("off");
        Button2.print("off");
        Button3.print("off");
        Button4.print("on");
        Button5.print("off");
    }
    else if (mode == BLINKER_CMD_ALIGENIE_COMMON)
    {
        // Your mode function
    }
    TMwsMode = mode;
    BlinkerAliGenie.mode(TMwsMode);
    BlinkerAliGenie.print();
}

void aligeniecMode(const String &cmode)
{
    BLINKER_LOG("need cancel mode: ", cmode);
    if (cmode == BLINKER_CMD_ALIGENIE_READING)
    {
        // Your mode function
        BlinkerAliGenie.powerState("on");
        wsState = true;
        colorR = 255;
        colorG = 255;
        colorB = 255;
        BRIGHTNESS = 255;
        openState = 1;
        Button1.print("on");
        Button2.print("off");
        Button3.print("off");
        Button4.print("off");
        Button5.print("off");
    }
    else if (cmode == BLINKER_CMD_ALIGENIE_MOVIE)
    {
        // Your mode function
        BlinkerAliGenie.powerState("on");
        wsState = true;
        colorR = 255;
        colorG = 255;
        colorB = 255;
        BRIGHTNESS = 255;
        openState = 1;
        Button1.print("on");
        Button2.print("off");
        Button3.print("off");
        Button4.print("off");
        Button5.print("off");
    }
    else if (cmode == BLINKER_CMD_ALIGENIE_SLEEP)
    {
        // Your mode function
    }
    else if (cmode == BLINKER_CMD_ALIGENIE_HOLIDAY)
    {
        // Your mode function
        BlinkerAliGenie.powerState("on");
        wsState = true;
        colorR = 255;
        colorG = 255;
        colorB = 255;
        BRIGHTNESS = 255;
        openState = 1;
        Button1.print("on");
        Button2.print("off");
        Button3.print("off");
        Button4.print("off");
        Button5.print("off");
    }
    else if (cmode == BLINKER_CMD_ALIGENIE_MUSIC)
    {
        // Your mode function
        BlinkerAliGenie.powerState("on");
        wsState = true;
        colorR = 255;
        colorG = 255;
        colorB = 255;
        BRIGHTNESS = 255;
        openState = 1;
        Button1.print("on");
        Button2.print("off");
        Button3.print("off");
        Button4.print("off");
        Button5.print("off");
    }
    else if (cmode == BLINKER_CMD_ALIGENIE_COMMON)
    {
        // Your mode function
    }

    TMwsMode = cmode;               // new mode
    BlinkerAliGenie.mode(TMwsMode); // must response
    BlinkerAliGenie.brightness(BRIGHTNESS * 100 / 255);
    BlinkerAliGenie.print();
}

void aligenieBright(const String &bright)
{
    BLINKER_LOG("need set brightness: ", bright);

    if (bright == BLINKER_CMD_MAX)
    {
        BRIGHTNESS = 255;
    }
    else if (bright == BLINKER_CMD_MIN)
    {
        BRIGHTNESS = 0;
    }
    else
    {
        BRIGHTNESS = bright.toInt() * 255 / 100;
    }

    BLINKER_LOG("now set brightness: ", BRIGHTNESS);

    BlinkerAliGenie.brightness(BRIGHTNESS * 100 / 255);
    Slider1.print(BRIGHTNESS);
    RGBWS2812.print(colorR, colorG, colorB, BRIGHTNESS);
    BlinkerAliGenie.print();
}

void aligenieRelativeBright(int32_t bright)
{
    BLINKER_LOG("need set relative brightness: ", bright);

    if (BRIGHTNESS + bright < 255 && BRIGHTNESS + bright >= 0)
    {
        BRIGHTNESS += bright;
    }

    BLINKER_LOG("now set brightness: ", BRIGHTNESS);

    BlinkerAliGenie.brightness(BRIGHTNESS * 100 / 255);
    Slider1.print(BRIGHTNESS);
    RGBWS2812.print(colorR, colorG, colorB, BRIGHTNESS);
    BlinkerAliGenie.print();
}

void aligenieColoTemp(int32_t colorTemp)
{
    BLINKER_LOG("need set colorTemperature: ", colorTemp);
    colorT = colorTemp;
    BlinkerAliGenie.colorTemp(colorT);
    BlinkerAliGenie.print();
}

void aligenieRelativeColoTemp(int32_t colorTemp)
{
    BLINKER_LOG("need set relative colorTemperature: ", colorTemp);
    colorT = colorTemp;
    BlinkerAliGenie.colorTemp(colorT);
    BlinkerAliGenie.print();
}

void aligenieQuery(int32_t queryCode)
{
    BLINKER_LOG("AliGenie Query codes: ", queryCode);

    switch (queryCode)
    {
    case BLINKER_CMD_QUERY_ALL_NUMBER:
        BLINKER_LOG("AliGenie Query All");
        BlinkerAliGenie.powerState(wsState ? "on" : "off");
        BlinkerAliGenie.color(TMgetColor());
        BlinkerAliGenie.mode(TMwsMode);
        BlinkerAliGenie.brightness(BRIGHTNESS * 100 / 255);
        BlinkerAliGenie.colorTemp(colorT);
        BlinkerAliGenie.print();
        break;
    case BLINKER_CMD_QUERY_POWERSTATE_NUMBER:
        BLINKER_LOG("AliGenie Query Power State");
        BlinkerAliGenie.powerState(wsState ? "on" : "off");
        BlinkerAliGenie.print();
        break;
    case BLINKER_CMD_QUERY_COLOR_NUMBER:
        BLINKER_LOG("AliGenie Query Color");
        BlinkerAliGenie.color(TMgetColor());
        BlinkerAliGenie.print();
        break;
    case BLINKER_CMD_QUERY_MODE_NUMBER:
        BLINKER_LOG("AliGenie Query Mode");
        BlinkerAliGenie.mode(TMwsMode);
        BlinkerAliGenie.print();
        break;
    case BLINKER_CMD_QUERY_BRIGHTNESS_NUMBER:
        BLINKER_LOG("AliGenie Query Brightness");
        BlinkerAliGenie.brightness(BRIGHTNESS * 100 / 255);
        BlinkerAliGenie.print();
        break;
    case BLINKER_CMD_QUERY_COLORTEMP_NUMBER:
        BLINKER_LOG("AliGenie Query ColorTemperature");
        BlinkerAliGenie.colorTemp(colorT);
        BlinkerAliGenie.print();
        break;
    default:
        BlinkerAliGenie.powerState(wsState ? "on" : "off");
        BlinkerAliGenie.color(TMgetColor());
        BlinkerAliGenie.mode(TMwsMode);
        BlinkerAliGenie.brightness(BRIGHTNESS * 100 / 255);
        BlinkerAliGenie.colorTemp(colorT);
        BlinkerAliGenie.print();
        break;
    }
}

void dataRead(const String &data)
{
    BLINKER_LOG("Blinker readString: ", data);

    Blinker.vibrate();

    uint32_t BlinkerTime = millis();

    Blinker.print("millis", BlinkerTime);
}

///////////////////////////////////////////////////////////////////////////K1灯效FastLED(依赖K2灯效FastLED)

typedef void (*SimplePatternList1[])();
SimplePatternList1 gPatterns1 = {rainbow}; //rainbow, rainbowWithGlitter, confetti, sinelon, juggle, bpm 6种特效切换

///////////////////////////////////////////////////////////////////////////K2灯效FastLED
// List of patterns to cycle through.  Each is defined as a separate function below.
typedef void (*SimplePatternList[])();
SimplePatternList gPatterns = {juggle}; //rainbow, rainbowWithGlitter, confetti, sinelon, juggle, bpm 6种特效切换

uint8_t gCurrentPatternNumber = 0; // Index number of which pattern is current
uint8_t gHue = 0;                  // rotating "base color" used by many of the patterns

#define ARRAY_SIZE(A) (sizeof(A) / sizeof((A)[0]))

void nextPattern()
{
    // add one to the current pattern number, and wrap around at the end
    gCurrentPatternNumber = (gCurrentPatternNumber + 1) % ARRAY_SIZE(gPatterns);
}

void rainbow()
{
    // FastLED's built-in rainbow generator
    fill_rainbow(leds, NUM_LEDS, gHue, 3);
}

void rainbowWithGlitter()
{
    // built-in FastLED rainbow, plus some random sparkly glitter
    rainbow();
    addGlitter(80);
}

void addGlitter(fract8 chanceOfGlitter)
{
    if (random8() < chanceOfGlitter)
    {
        leds[random16(NUM_LEDS)] += CRGB::White;
    }
}

void confetti()
{
    // random colored speckles that blink in and fade smoothly
    fadeToBlackBy(leds, NUM_LEDS, 10);
    int pos = random16(NUM_LEDS);
    leds[pos] += CHSV(gHue + random8(64), 200, 255);
}

void sinelon()
{
    // a colored dot sweeping back and forth, with fading trails
    fadeToBlackBy(leds, NUM_LEDS, 20);
    int pos = beatsin16(13, 0, NUM_LEDS - 1);
    leds[pos] += CHSV(gHue, 255, 192);
}

void bpm()
{
    // colored stripes pulsing at a defined Beats-Per-Minute (BPM)
    uint8_t BeatsPerMinute = 62;
    CRGBPalette16 palette = PartyColors_p;
    uint8_t beat = beatsin8(BeatsPerMinute, 64, 255);
    for (int i = 0; i < NUM_LEDS; i++)
    { //9948
        leds[i] = ColorFromPalette(palette, gHue + (i * 2), beat - gHue + (i * 10));
    }
}

void juggle()
{
    // eight colored dots, weaving in and out of sync with each other
    fadeToBlackBy(leds, NUM_LEDS, 20);
    byte dothue = 0;
    for (int i = 0; i < 8; i++)
    {
        leds[beatsin16(i + 7, 0, NUM_LEDS - 1)] |= CHSV(dothue, 200, 255);
        dothue += 32;
    }
}

///////////////////////////////////////////////////////////////////////////K3灯效
//冷却：空气上升时会冷却多少？
//冷却少=火焰高。 冷却更多=火焰更短。
//默认值50，建议范围20-100
#define COOLING 50

//发出火花：有什么机会（总共255个）点燃新的火花？
//更高的机会=更多的怒吼。 机会降低=火势更加闪烁。
//默认值为120，建议范围为50-200。
#define SPARKING 120
bool gReverseDirection = false;
void Fire2012()
{
    // Array of temperature readings at each simulation cell
    static byte heat[NUM_LEDS];

    // Step 1.  Cool down every cell a little
    for (int i = 0; i < NUM_LEDS; i++)
    {
        heat[i] = qsub8(heat[i], random8(0, ((COOLING * 10) / NUM_LEDS) + 2));
    }

    // Step 2.  Heat from each cell drifts 'up' and diffuses a little
    for (int k = NUM_LEDS - 1; k >= 2; k--)
    {
        heat[k] = (heat[k - 1] + heat[k - 2] + heat[k - 2]) / 3;
    }

    // Step 3.  Randomly ignite new 'sparks' of heat near the bottom
    if (random8() < SPARKING)
    {
        int y = random8(7);
        heat[y] = qadd8(heat[y], random8(160, 255));
    }

    // Step 4.  Map from heat cells to LED colors
    for (int j = 0; j < NUM_LEDS; j++)
    {
        CRGB color = HeatColor(heat[j]);
        int pixelnumber;
        if (gReverseDirection)
        {
            pixelnumber = (NUM_LEDS - 1) - j;
        }
        else
        {
            pixelnumber = j;
        }
        leds[pixelnumber] = color;
    }
}

///////////////////////////////////////////////////////////////////////////K4灯效
#if FASTLED_VERSION < 3001000
#error "Requires FastLED 3.1 or later; check github for latest code."
#endif
// This function draws rainbows with an ever-changing,
// widely-varying set of parameters.
void pride()
{
    static uint16_t sPseudotime = 0;
    static uint16_t sLastMillis = 0;
    static uint16_t sHue16 = 0;

    uint8_t sat8 = beatsin88(87, 220, 250);
    uint8_t brightdepth = beatsin88(341, 96, 224);
    uint16_t brightnessthetainc16 = beatsin88(203, (25 * 256), (40 * 256));
    uint8_t msmultiplier = beatsin88(147, 23, 60);

    uint16_t hue16 = sHue16; //gHue * 256;
    uint16_t hueinc16 = beatsin88(113, 1, 3000);

    uint16_t ms = millis();
    uint16_t deltams = ms - sLastMillis;
    sLastMillis = ms;
    sPseudotime += deltams * msmultiplier;
    sHue16 += deltams * beatsin88(400, 5, 9);
    uint16_t brightnesstheta16 = sPseudotime;

    for (uint16_t i = 0; i < NUM_LEDS; i++)
    {
        hue16 += hueinc16;
        uint8_t hue8 = hue16 / 256;

        brightnesstheta16 += brightnessthetainc16;
        uint16_t b16 = sin16(brightnesstheta16) + 32768;

        uint16_t bri16 = (uint32_t)((uint32_t)b16 * (uint32_t)b16) / 65536;
        uint8_t bri8 = (uint32_t)(((uint32_t)bri16) * brightdepth) / 65536;
        bri8 += (255 - brightdepth);

        CRGB newcolor = CHSV(hue8, sat8, bri8);

        uint16_t pixelnumber = i;
        pixelnumber = (NUM_LEDS - 1) - pixelnumber;

        nblend(leds[pixelnumber], newcolor, 64);
    }
}
void mode_2()
{
    if (BRIGHTNESS < 20)
    {
        BRIGHTNESS = 20;
        Slider1.print(BRIGHTNESS);
    }
    FastLED.setBrightness(BRIGHTNESS); //实时更新亮度
    gPatterns1[gCurrentPatternNumber]();
    FastLED.show();
    FastLED.delay(200 / UPDATES_PER_SECOND);
    EVERY_N_MILLISECONDS(20) { gHue++; }   // slowly cycle the "base color" through the rainbow
    EVERY_N_SECONDS(10) { nextPattern(); } // change patterns periodically
}

void mode_3()
{
    if (BRIGHTNESS < 20)
    {
        BRIGHTNESS = 20;
        Slider1.print(BRIGHTNESS);
    }
    FastLED.setBrightness(BRIGHTNESS); //实时更新亮度
    gPatterns[gCurrentPatternNumber]();
    FastLED.show();
    FastLED.delay(1000 / (UPDATES_PER_SECOND / 10));

    // do some periodic updates
    EVERY_N_MILLISECONDS(20) { gHue++; }   // slowly cycle the "base color" through the rainbow
    EVERY_N_SECONDS(10) { nextPattern(); } // change patterns periodically
}

void mode_4()
{
    if (BRIGHTNESS < 20)
    {
        BRIGHTNESS = 20;
        Slider1.print(BRIGHTNESS);
    }
    Fire2012();
    FastLED.setBrightness(BRIGHTNESS); //实时更新亮度
    FastLED.delay(1000 / UPDATES_PER_SECOND);
}

void mode_5()
{
    if (BRIGHTNESS < 20)
    {
        BRIGHTNESS = 20;
        Slider1.print(BRIGHTNESS);
    }
    FastLED.setBrightness(BRIGHTNESS); //实时更新亮度
    pride();
    FastLED.show();
    FastLED.delay(1000 / (UPDATES_PER_SECOND / 5));
}
void setup()
{
    Serial.begin(115200);
    BLINKER_DEBUG.stream(Serial);
    //BLINKER_DEBUG.debugAll();

    pinMode(USB_F2, OUTPUT);
    digitalWrite(USB_F2, LOW);

    Button1.attach(button1_callback);
    Button2.attach(button2_callback);
    Button3.attach(button3_callback);
    Button4.attach(button4_callback);
    Button5.attach(button5_callback);
    Slider1.attach(slider1_callback);
    Slider2.attach(slider2_callback);
    RGBWS2812.attach(ws2812_callback);
    Blinker.attachHeartbeat(heartbeat);

    Blinker.begin(auth, ssid, pswd);
    Blinker.attachData(dataRead);

    BlinkerMIOT.attachPowerState(miotPowerState);
    BlinkerMIOT.attachColor(miotColor);
    BlinkerMIOT.attachMode(miotMode);
    BlinkerMIOT.attachBrightness(miotBright);
    BlinkerMIOT.attachColorTemperature(miotColoTemp);
    BlinkerMIOT.attachQuery(miotQuery);

    BlinkerDuerOS.attachPowerState(duerPowerState);
    BlinkerDuerOS.attachColor(duerColor);
    BlinkerDuerOS.attachMode(duerMode);
    BlinkerDuerOS.attachCancelMode(duercMode);
    BlinkerDuerOS.attachBrightness(duerBright);
    BlinkerDuerOS.attachRelativeBrightness(duerRelativeBright);
    BlinkerDuerOS.attachColorTemperature(duerColoTemp);
    BlinkerDuerOS.attachRelativeColorTemperature(duerRelativeColoTemp);
    BlinkerDuerOS.attachQuery(duerQuery);

    BlinkerAliGenie.attachPowerState(aligeniePowerState);
    BlinkerAliGenie.attachColor(aligenieColor);
    BlinkerAliGenie.attachMode(aligenieMode);
    BlinkerAliGenie.attachCancelMode(aligeniecMode);
    BlinkerAliGenie.attachBrightness(aligenieBright);
    BlinkerAliGenie.attachRelativeBrightness(aligenieRelativeBright);
    BlinkerAliGenie.attachColorTemperature(aligenieColoTemp);
    BlinkerAliGenie.attachRelativeColorTemperature(aligenieRelativeColoTemp);
    BlinkerAliGenie.attachQuery(aligenieQuery);

    FastLED.addLeds<LED_TYPE, PIN, COLOR_ORDER>(leds, NUM_LEDS)
        .setCorrection(TypicalLEDStrip)
        .setDither(BRIGHTNESS < 255);
    currentPalette = RainbowColors_p;
    currentBlending = LINEARBLEND;
}

void loop()
{
    Blinker.run();

    switch (openState)
    {
    case 0:
        colorR = 0;
        colorG = 0;
        colorB = 0;
        BRIGHTNESS = 0;
        RGBWShow();
        digitalWrite(USB_F2, LOW);
        break;
    case 1:
        RGBWShow();
        digitalWrite(USB_F2, HIGH);
        break;
    case 2:
        mode_2();
        digitalWrite(USB_F2, HIGH);
        break;
    case 3:
        mode_3();
        digitalWrite(USB_F2, HIGH);
        break;
    case 4:
        mode_4();
        digitalWrite(USB_F2, HIGH);
        break;
    case 5:
        mode_5();
        digitalWrite(USB_F2, HIGH);
        break;
    default:
        break;
    }
}
